/*
 * Main.java
 *
 * Created on September 5, 2006, 8:41 AM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */
package smfbackup;

import java.awt.Desktop;
import java.awt.Frame;
import java.io.IOException;
import javax.swing.RepaintManager;
import javax.swing.UIManager;
import org.apache.log4j.FileAppender;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.apache.log4j.PatternLayout;
import org.apache.log4j.Priority;

/**
 *
 * @author Iulian
 */
public class Main {

    private static final MainWindow mw = new MainWindow();

    /** Creates a new instance of Main */
    public Main() {
    }

    /**
     * No arguments are required to start up the application.
     *
     * @param args the command line arguments
     */
    public static void main(String[] args) {

        initLogging();

        UIManager.installLookAndFeel("Liquid", "com.birosoft.liquid.LiquidLookAndFeel");

        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {

                // enable global double buffer
                RepaintManager currentManager = RepaintManager.currentManager(mw);
                currentManager.setDoubleBufferingEnabled(true);

                Utils.centreFrameOnScreen(mw);
                
                if (mw.isStartMinimized()) {
                    mw.setState(Frame.ICONIFIED);
                    mw.setVisible(false);
                }
                else
                    mw.setVisible(true);
            }
        });
    }

    /*
     * Initialize the logging. All messages only will pass through till
     * the configuration will kick in and update the logging per user's request
     */
    private static void initLogging() {
        try {
            FileAppender fa = new FileAppender();
            fa.setLayout(new PatternLayout("%d [%15.15t] %-5p %30.30c %x - %m%n"));
            fa.setName("root");
            fa.setFile("smfbackup.log", false, false, 1024);
            fa.setThreshold(Priority.DEBUG);
            Logger.getRootLogger().addAppender(fa);

            // disable anoying httpclient logging
            Logger.getLogger("httpclient.wire.header").setLevel(Level.INFO);
            Logger.getLogger("httpclient.wire.content").setLevel(Level.INFO);

        } catch (IOException ex) {
            ex.printStackTrace();
            System.err.println("Cannot init logging system, quitting.");
            System.exit(2);
        }
    }

    public static void setLoggingLevel(String level) {
        if ("Only Severe".equalsIgnoreCase(level)) {
            Logger.getRootLogger().setPriority(Priority.INFO);
        } else {
            Logger.getRootLogger().setPriority(Priority.DEBUG);
        }
    }

    public static MainWindow getMainWindow() {
        return mw;
    }
}
